package com.sec.android.app.myfiles.module.suggestion;

import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.home.HomeFragment;
import com.sec.android.app.myfiles.fragment.home.HomeFragmentItem;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.ModuleLoader;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.widget.listview.ResizeListView;

import java.util.ArrayList;

public class SuggestionHomeItem extends HomeFragmentItem implements AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
    private ResizeListView mListView;
    private SuggestionAdapterImp mAdapterImp;
    private View mLayoutContainer;

    public SuggestionHomeItem(Context context, HomeFragment fragment) {
        super(context, fragment);
    }

    @Override
    protected void setListItem() {
        NavigationManager naviMgr = NavigationManager.getInstance(mFragment.getProcessId());
        NavigationInfo curInfo = naviMgr.getCurInfo();
        if (curInfo != null) {
            NavigationInfo.NavigationMode naviMode = curInfo.getNavigationMode();
            mLayoutContainer = mRootView.findViewById(R.id.home_suggestion_folder);
            if (naviMode == NavigationInfo.NavigationMode.Select_copy_destination || naviMode == NavigationInfo.NavigationMode.Select_move_destination) {
                if (mLayoutContainer != null) {
                    mListView = (ResizeListView) mLayoutContainer.findViewById(R.id.suggestion_list);
                    mAdapterImp = new SuggestionAdapterImp(mContext, null, R.layout.home_list_item, null, 0);
                    mListView.setAdapter(mAdapterImp);
                    mListView.setOnItemClickListener(this);
                    mListView.setFocusableInTouchMode(false);

                    try {
                        mFragment.getLoaderManager().initLoader(200, null, this);
                    } catch (IllegalStateException e) {
                        Log.e(this, "IllegalStateException:" + e.toString());
                    }
                }
            }
        }
    }

    @Override
    protected void _onResume() {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SuggestionFileRecord shortcutRecord = (SuggestionFileRecord) mAdapterImp.getFileRecord(position);
        if (shortcutRecord != null) {
            FileRecord record = ModuleLoader.getInstance().createFileRecord(shortcutRecord.getRealStorageType(), shortcutRecord.getFullPath());
            MyFilesFacade.executeRecord(mFragment.getProcessId(), mFragment.getActivity(), record);
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
        CursorLoader cl = new CursorLoader(mContext, mAdapterImp.getUri(), null, mAdapterImp.getSelection(), null, mAdapterImp.getOrderBy());
        return cl;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (mAdapterImp != null) {
            if (data != null) {
                if (data.getCount() > 0) {
                    showSuggestionFolder(true);
                    updateSuggestionItem(data);
                    mAdapterImp.changeCursor(data);
                    return;
                }
            } else {
                mAdapterImp.changeCursor(null);
            }
        }
        showSuggestionFolder(false);
    }

    private void showSuggestionFolder(boolean show) {
        setViewVisibility(mLayoutContainer, show ? View.VISIBLE : View.GONE);
    }

    public void updateSuggestionItem(Cursor data) {
        if (data != null && data.moveToFirst()) {
            ArrayList<FileRecord> records = new ArrayList<>();
            do {
                FileRecord record = mAdapterImp.getFileRecord(data);
                if (record != null) {
                    if (!record.exists(mContext)) {
                        records.add(record);
                    }
                }
            } while (data.moveToNext());

            if (!records.isEmpty()) {
                MyFilesFacade.deleteSuggestion(mFragment.getProcessId(), mContext, records);
                reload();
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
    }

    @Override
    protected void _onDestroy() {
    }

    @Override
    protected void _onPause() {
    }

    @Override
    protected void setItemEnabled(boolean enabled) {
        setChildItemEnabled(mListView, enabled);
    }

    @Override
    protected void setChildItemEnabled(View view, boolean bEnable) {
        super.setChildItemEnabled(view, bEnable);
        if (mAdapterImp != null) {
            if (mAdapterImp.getCursor() != null && !mAdapterImp.getCursor().isClosed()) {
                mAdapterImp.setEnabled(bEnable);
                if (mListView != null) {
                    mListView.setEnabled(bEnable);
                    mListView.invalidateViews();
                }
            }
        }
    }

    public void reload() {
        if (mFragment.isAdded() == true) {
            mFragment.getLoaderManager().restartLoader(200, null, this);
        }
    }
}
